#include "bluetooth_app.h"
#include "bluetooth_board.h"
#include "bluetooth_peripheral_service.h"
#include "CH58xBLE_ROM.h"
#include "tim.h"
// #include "peripheral.h"
// #include "gattprofile.h"
//#include "app.h"
/**
 * @brief 与主机建立链接后执行的操作
 *
 */
static void ActionsPerformedAfterEstablishingLinkWithTheHost(void);

/**
 * @brief 与主机断开链接后执行的操作
 *
 */
static void ActionsPerformedAfterDisconnectingFromTheHost(void);

/**
 * @brief 加载相关参数
 *
 */
 void PeripheralLoadRelatedParameters(void);

/**
 * @brief 重置蓝牙连接时间
 *
 */
static void ResetTheBluetoothConnectionTime(void);

/**
 * @brief 重置蓝牙报文接收时间
 *
 */
static void ExampleResetTheTimeForReceivingBluetoothPackets(void);

/**
 * @brief 发起通知
 *
 * @param pDataIn - 数据报文
 * @param SizeIn  - 数据报文有效长度
 */
//static void PeripheralNotify(const uint8_t *pDataIn, uint16_t SizeIn);

/**
 * @brief 初始化连接项目
 *
 */
 void PeripheralInitConnItem(void);

/**
 * @brief 进程链接已建立
 *
 * @param pEvent - 事件句柄
 */
static void PeripheralLinkEstablished(gapRoleEvent_t *pEvent);

/**
 * @brief 进程链接终止
 *
 * @param pEvent - 事件句柄
 */
static void PeripheralLinkTerminated(gapRoleEvent_t *pEvent);

/**
 * @brief 来自配置文件的状态更改通知
 *
 * @param NewState      - 新状态
 * @param pGapRoleEvent - 事件回调
 */
static void PeripheralStateNotificationCallback(gapRole_States_t NewState, gapRoleEvent_t *pGapRoleEvent);

/**
 * @brief 获取当前通信质量
 *
 * @param ConnHandle - 连接句柄
 * @param RSSI       - RSSI
 */
static void PeripheralRssiCallback(uint16_t ConnHandle, int8_t RSSI);

/**
 * @brief 处理传入的任务消息
 *
 * @param pEvent - 任务消息
 */
static void PeripheralProcessGAPMsg(gapRoleEvent_t *pEvent);

/**
 * @brief 参数更新完成处理任务
 *
 * @param ConnHandle       - 连接句柄
 * @param ConnInterval     - 连接间隔
 * @param ConnSlaveLatency - 连接从设备延时
 * @param ConnTimeout      - 连接超时
 */
static void PeripheralParamUpdateCallback(uint16_t ConnHandle, uint16_t ConnInterval, uint16_t ConnSlaveLatency, uint16_t ConnTimeout);

/**
 * @brief 主机写入数据处理任务
 *
 * @param pDataIn - 主机写入的数据
 * @param SizeIn  - 写入数据有效长度
 */
  void PeripheralHostWriteDataHandler(const uint8_t *pDataIn, uint16_t SizeIn);

/**
 * @brief 任务消息处理
 *
 * @param pMsg - 消息
 */
static void PeripheralProcessTMOSMsg(tmos_event_hdr_t *pMsg);

/**
 * @brief 更新蓝牙广播包
 *
 */
static void BluetoothUpdatingTheBroadcastPackage(void);

extern void App_peripheral_reveived(uint8_t *pValue, uint16_t len);
//static uint8_t attDeviceName1[GAP_DEVICE_NAME_LEN] = "Simple Peripheral";
/**
 * @brief 蓝牙事件处理函数
 *
 * @param TaskId - 任务句柄
 * @param Events - 事件类型
 */
 uint16_t PeripheralProcessEvent(uint8_t TaskId, uint16_t Events);
void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
                                    uint16_t connSlaveLatency, uint16_t connTimeout);

bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti);
void peripheralChar6Notify(uint8_t *pValue, uint16_t len);
uint8_t NumberOfAttDevice = 0;
uint8_t NumberOfAdvertData = 0;
uint8_t NumberOfScanRspData = 0;

uint8_t PeripheralTaskId = INVALID_TASK_ID;
uint8_t PeripheralMTU = PERIPHERAL_CHARACTERISTIC_LENGTH;

uint8_t AdvertData[B_MAX_ADV_LEN] = {0};
uint8_t ScanRspData[B_MAX_ADV_LEN] = {0};
uint8_t AttDeviceName[GAP_DEVICE_NAME_LEN] = {0};
uint8_t LinkPermission=1;

BluetoothDataBuff_t BluetoothDataBuff = {0};
PeripheralConnItem_t PeripheralConnList = {0};

// GAP Role Callbacks
 gapRolesCBs_t PeripheralPeripheralCallback = {
    PeripheralStateNotificationCallback,
    PeripheralRssiCallback,
    PeripheralParamUpdateCallback,
};

// Broadcast Callbacks
 gapRolesBroadcasterCBs_t BroadcasterBroadcasterCallback = {
    NULL,
    NULL,
};

// GAP Bond Manager Callbacks
static gapBondCBs_t PeripheralBondMgrCallback = {
    NULL,
    NULL,
};


static gapRolesBroadcasterCBs_t Broadcaster_BroadcasterCBs = {
    NULL, // Not used in peripheral role
    NULL  // Receive scan request callback
};
static void ActionsPerformedAfterEstablishingLinkWithTheHost(void)
{
  BluetoothPostConnectionActions();
}

static void ActionsPerformedAfterDisconnectingFromTheHost(void)
{
  BluetoothPostDisconnectionActions();
}

 void PeripheralLoadRelatedParameters(void)
{
  char HostName[GAP_DEVICE_NAME_LEN] = {0};
  uint8_t ChipMeshType[6]={0x05,                  // length of this data
    GAP_ADTYPE_MANUFACTURER_SPECIFIC,
    0xD7,
    0x07,
    0x00,
    0x00};
  uint8_t MacAddr[MAC_ADDR_CODING_LENGTH] = {0}, BatteryLevel = 0;
  uint8_t DeviceWorkMode = 0, HostNameLenght = 0, ProtocolVersion = 0, DeviceType = 0, ToBePushed = 0;

  BluetoothGetRadioName(HostName);
  BluetoothGetMacAddress(MacAddr);

  HostNameLenght = strlen(HostName);
  DeviceType = BluetoothGetDeviceType();
  printf("device type %0d\r\n",DeviceType);
  DeviceWorkMode = BluetoothGetSystemStatus();
  ProtocolVersion = BluetoothGetProtocolVersion();
  ToBePushed = BluetoothGetEventToBeSendFlagBit();
  BatteryLevel = BluetoothGetBatteyLevel();

  NumberOfAdvertData = 0;
  AdvertData[NumberOfAdvertData++] = 2;
  AdvertData[NumberOfAdvertData++] = GAP_ADTYPE_FLAGS;
  AdvertData[NumberOfAdvertData++] = DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED;

  AdvertData[NumberOfAdvertData++] = 3;
  AdvertData[NumberOfAdvertData++] = GAP_ADTYPE_16BIT_MORE;
  AdvertData[NumberOfAdvertData++] = LO_UINT16(PERIPHERAL_SERVICE_UUID);
  AdvertData[NumberOfAdvertData++] = HI_UINT16(PERIPHERAL_SERVICE_UUID);
//mesh chip factory
 // AdvertData[NumberOfAdvertData++] = 5;
//  AdvertData[NumberOfAdvertData++] = GAP_ADTYPE_MANUFACTURER_SPECIFIC;
 // AdvertData[NumberOfAdvertData++] = 0xD7;//(PERIPHERAL_SERVICE_UUID);
//  AdvertData[NumberOfAdvertData++] = 0x07;//HI_UINT16(PERIPHERAL_SERVICE_UUID);
 // AdvertData[NumberOfAdvertData++] = 0x00;//LO_UINT16(PERIPHERAL_SERVICE_UUID);
 // AdvertData[NumberOfAdvertData++] = 0x00;//HI_UINT16(PERIPHERAL_SERVICE_UUID);
  
  NumberOfScanRspData = 0;
  ScanRspData[NumberOfScanRspData++] = HostNameLenght + 1;
  ScanRspData[NumberOfScanRspData++] = GAP_ADTYPE_LOCAL_NAME_COMPLETE;
  memcpy(&ScanRspData[NumberOfScanRspData], HostName, HostNameLenght);
  NumberOfScanRspData += HostNameLenght;

  ScanRspData[NumberOfScanRspData++] = 5;
  ScanRspData[NumberOfScanRspData++] = GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE;
  ScanRspData[NumberOfScanRspData++] = LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL);
  ScanRspData[NumberOfScanRspData++] = HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL);
  ScanRspData[NumberOfScanRspData++] = LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL);
  ScanRspData[NumberOfScanRspData++] = HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL);

  ScanRspData[NumberOfScanRspData++] = 12;
  ScanRspData[NumberOfScanRspData++] = GAP_ADTYPE_MANUFACTURER_SPECIFIC;
  memcpy(&ScanRspData[NumberOfScanRspData], MacAddr, MAC_ADDR_CODING_LENGTH);
  NumberOfScanRspData += MAC_ADDR_CODING_LENGTH;
  ScanRspData[NumberOfScanRspData++] = DeviceWorkMode;
  ScanRspData[NumberOfScanRspData++] = ProtocolVersion;
  ScanRspData[NumberOfScanRspData++] = DeviceType;
  ScanRspData[NumberOfScanRspData++] = ToBePushed;
  ScanRspData[NumberOfScanRspData++] = BatteryLevel;

  NumberOfAttDevice = HostNameLenght;
  memcpy(AttDeviceName, HostName, HostNameLenght);
}

static void ResetTheBluetoothConnectionTime(void)
{
  uint16_t ConnectTimeout = BluetoothGetConnectTimeout();
  tmos_stop_task(PeripheralTaskId, BLUETOOTH_CONNECT_TIMEOUT_EVENT);
  tmos_start_task(PeripheralTaskId, BLUETOOTH_CONNECT_TIMEOUT_EVENT, ConnectTimeout);
}

static void ExampleResetTheTimeForReceivingBluetoothPackets(void)
{
  tmos_stop_task(PeripheralTaskId, BLUETOOTH_RECIVE_TIMEOUT_EVENT);
  tmos_start_task(PeripheralTaskId, BLUETOOTH_RECIVE_TIMEOUT_EVENT, DEFAULT_BLUETOOTH_RECIVE_TIMEOUT);
}



 void PeripheralInitConnItem(void)
{
  PeripheralConnList.ConnTimeout = 0;
  PeripheralConnList.ConnInterval = 0;
  PeripheralConnList.ConnSlaveLatency = 0;
  PeripheralConnList.ConnHandle = GAP_CONNHANDLE_INIT;
}

static void PeripheralLinkEstablished(gapRoleEvent_t *pEvent)
{
  gapEstLinkReqEvent_t *LinkEvent = (gapEstLinkReqEvent_t *)pEvent;

  if (PeripheralConnList.ConnHandle != GAP_CONNHANDLE_INIT)
  {
    PRINT("[bluetooth]: connection max.\n");
    GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle);
  }
  else
  {
    PRINT("[bluetooth]: established connection id: %02X, interval: %02X.\r\n", LinkEvent->connectionHandle, LinkEvent->connInterval);

    PeripheralMTU = PERIPHERAL_CHARACTERISTIC_LENGTH;
    PeripheralConnList.ConnTimeout = LinkEvent->connTimeout;
    PeripheralConnList.ConnInterval = LinkEvent->connInterval;
    PeripheralConnList.ConnHandle = LinkEvent->connectionHandle;
    PeripheralConnList.ConnSlaveLatency = LinkEvent->connLatency;

    ResetTheBluetoothConnectionTime();
    ActionsPerformedAfterEstablishingLinkWithTheHost();

    tmos_start_task(PeripheralTaskId, BLUETOOTH_PARAM_UPDATE_EVENT, SBP_PARAM_UPDATE_DELAY);
    tmos_start_task(PeripheralTaskId, BLUETOOTH_READ_RSSI_EVENT, SBP_READ_RSSI_EVT_PERIOD);
  }
}

static void PeripheralLinkTerminated(gapRoleEvent_t *pEvent)
{
  gapTerminateLinkEvent_t *LinkEvent = (gapTerminateLinkEvent_t *)pEvent;
  if (LinkEvent->connectionHandle == PeripheralConnList.ConnHandle)
  {
    PeripheralConnList.ConnTimeout = 0;
    PeripheralConnList.ConnInterval = 0;
    PeripheralConnList.ConnSlaveLatency = 0;
    PeripheralConnList.ConnHandle = GAP_CONNHANDLE_INIT;

    uint8_t AdvertisingEnable = true;
    GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, 1, &AdvertisingEnable);
  }
}


static void PeripheralStateNotificationCallback(gapRole_States_t NewState, gapRoleEvent_t *pGapRoleEvent)
{
  switch (NewState & GAPROLE_STATE_ADV_MASK)
  {
  case GAPROLE_STARTED:
  {
    PRINT("[bluetooth]: initialized.\r\n");
  }
  break;

  case GAPROLE_ADVERTISING:
  {
    PRINT("[bluetooth]: advertising.\r\n");
    if (pGapRoleEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
    {
      printf("123123\r\n");
      // if(LinkPermission)
      {
        PeripheralLinkTerminated(pGapRoleEvent);
      }
    }

  }
  break;

  case GAPROLE_CONNECTED:
  {
    if (pGapRoleEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
    {
      // if(LinkPermission)
      {
          PRINT("[bluetooth]: connected.\r\n");
          Stop_Close_Scan_Task();
          PeripheralLinkEstablished(pGapRoleEvent);
      }
    }
  }
  break;

  case GAPROLE_CONNECTED_ADV:
  {
    PRINT("[bluetooth]: connected advertising.\r\n");
  }
  break;

  case GAPROLE_WAITING:
  {
    if (pGapRoleEvent->gap.opcode == GAP_END_DISCOVERABLE_DONE_EVENT)
    {
      PRINT("[bluetooth]: waiting for advertising.\r\n");
      tmos_start_task(PeripheralTaskId, BLUETOOTH_UPDATING_THE_BROADCAST_PACKAGE_EVENT, BLUETOOTH_UPDATE_RADIO_PACKAGE_TIME);
    }
    else if (pGapRoleEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
    {
      PRINT("[bluetooth]: disconnected, reason: %x.\r\n", pGapRoleEvent->linkTerminate.reason);
      Start_Close_Scan_Task();
      PeripheralLinkTerminated(pGapRoleEvent);
      ActionsPerformedAfterDisconnectingFromTheHost();
      LinkPermission=0;
    }
    else if (pGapRoleEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
    {
      if (pGapRoleEvent->gap.hdr.status != SUCCESS)
      {
        PRINT("[bluetooth]: waiting for advertising.\r\n");
        tmos_start_task(PeripheralTaskId, BLUETOOTH_UPDATING_THE_BROADCAST_PACKAGE_EVENT, BLUETOOTH_UPDATE_RADIO_PACKAGE_TIME);
      }
    }
  }
  break;

  default:
    break;
  }
}

static void PeripheralRssiCallback(uint16_t ConnHandle, int8_t Rssi)
{
  PRINT("[bluetooth]: peripheral rssi: -%d dB connection id: %02X.\r\n", -Rssi, ConnHandle);
}

static void PeripheralProcessGAPMsg(gapRoleEvent_t *pEvent)
{
  switch (pEvent->gap.opcode)
  {
  case GAP_SCAN_REQUEST_EVENT:
  {
    PRINT("[bluetooth]: received a scan from: %X-%X-%X-%X-%X%X.\r\n", pEvent->scanReqEvt.scannerAddr[0],
          pEvent->scanReqEvt.scannerAddr[1], pEvent->scanReqEvt.scannerAddr[2], pEvent->scanReqEvt.scannerAddr[3],
          pEvent->scanReqEvt.scannerAddr[4], pEvent->scanReqEvt.scannerAddr[5]);
  }
  break;

  case GAP_PHY_UPDATE_EVENT:
  {
    PRINT("[bluetooth]: phy update rx: %X tx: %X\r\n", pEvent->linkPhyUpdate.connRxPHYS, pEvent->linkPhyUpdate.connTxPHYS);
  }
  break;

  default:
    break;
  }
}

static void PeripheralParamUpdateCallback(uint16_t ConnHandle, uint16_t ConnInterval, uint16_t ConnSlaveLatency, uint16_t ConnTimeout)
{
  if (ConnHandle == PeripheralConnList.ConnHandle)
  {
    PeripheralConnList.ConnTimeout = ConnTimeout;
    PeripheralConnList.ConnInterval = ConnInterval;
    PeripheralConnList.ConnSlaveLatency = ConnSlaveLatency;

    PRINT("[bluetooth]: update connection id: %02X, interval: %02X.\r\n", ConnHandle, ConnInterval);
  }
}

 void PeripheralHostWriteDataHandler(const uint8_t *pDataIn, uint16_t SizeIn)
{
  if ((BluetoothDataBuff.DataSize + SizeIn) < BLUETOOTH_MESSAGE_PACKET_LENGTH)
  {
    memcpy(&BluetoothDataBuff.DataBuff[BluetoothDataBuff.DataSize], pDataIn, SizeIn);
    BluetoothDataBuff.DataSize += SizeIn;
  }

  ResetTheBluetoothConnectionTime();
  ExampleResetTheTimeForReceivingBluetoothPackets();
}

static void PeripheralProcessTMOSMsg(tmos_event_hdr_t *pMsg)
{
  switch (pMsg->event)
  {
  case GAP_MSG_EVENT:
  {
    PeripheralProcessGAPMsg((gapRoleEvent_t *)pMsg);
  }
  break;

  case GATT_MSG_EVENT:
  {
    gattMsgEvent_t *pMsgEvent = (gattMsgEvent_t *)pMsg;

    if (pMsgEvent->method == ATT_MTU_UPDATED_EVENT)
    {
      PeripheralMTU = pMsgEvent->msg.exchangeMTUReq.clientRxMTU;
      PRINT("[bluetooth]: mtu exchange: %d\r\n", PeripheralMTU);
    }
  }
  break;

  default:
    break;
  }
}

static void BluetoothUpdatingTheBroadcastPackage(void)
{
  uint16_t DesiredMinInterval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
  uint16_t DesiredMaxInterval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;

  GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, 1, (uint8_t[]){false});
  PeripheralLoadRelatedParameters();
  // GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, 1, (uint8_t[]){true});
  GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, NumberOfScanRspData, ScanRspData);
  GAPRole_SetParameter(GAPROLE_ADVERT_DATA, NumberOfAdvertData, AdvertData);
  GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &DesiredMinInterval);
  GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &DesiredMaxInterval);
}

 uint16_t PeripheralProcessEvent(uint8_t TaskId, uint16_t Events)
{
  // printf("123123963\r\n");

  if ((TaskId == PeripheralTaskId))//&&((LinkPermission==1)||(LinkPermission==3)))
  {
    // printf("456456456\r\n");
    // if(LinkPermission==1)
    // {
    //   LinkPermission=0;
    //   printf("close PeripheralProcessEvent\r\n");
    // }
    if (Events & SYS_EVENT_MSG)
    {
      uint8_t *pMessageHandler;
      printf("987987987\r\n");
      pMessageHandler= tmos_msg_receive(PeripheralTaskId);
      if (pMessageHandler != NULL)
      {
//       PeripheralProcessTMOSMsg((tmos_event_hdr_t *)pMessageHandler);
      // printf("123123456789123456789\r\n");
      
      tmos_msg_deallocate(pMessageHandler);
       }

      return (Events ^ SYS_EVENT_MSG);
    }

    if (Events & BLUETOOTH_CONNECT_TIMEOUT_EVENT)
    {
      printf("456456\r\n");
      GAPRole_TerminateLink(PeripheralConnList.ConnHandle);
      return (Events ^ BLUETOOTH_CONNECT_TIMEOUT_EVENT);
    }

    if (Events & BLUETOOTH_RECIVE_TIMEOUT_EVENT)
    {
      // uint8_t date[10]={0,1,2,3,4,5,6,7,8,9};
      BluetoothAnalysisServerPushMessage(BluetoothDataBuff.DataBuff, BluetoothDataBuff.DataSize);
      memset(&BluetoothDataBuff, 0, sizeof(BluetoothDataBuff_t));
      // peripheralChar6Notify(date, 10);
//      peripheralChar4Notify(date, 10);
      printf("789789\r\n");
      return (Events ^ BLUETOOTH_RECIVE_TIMEOUT_EVENT);
    }

    if (Events & BLUETOOTH_START_DEVICE_EVENT)
    {
       printf("987987\r\n");
      GAPRole_PeripheralStartDevice(PeripheralTaskId, &PeripheralBondMgrCallback, &PeripheralPeripheralCallback);
      return (Events ^ BLUETOOTH_START_DEVICE_EVENT);
    }

    if (Events & BLUETOOTH_PARAM_UPDATE_EVENT)
    {
      GAPRole_PeripheralConnParamUpdateReq(PeripheralConnList.ConnHandle,
                                           DEFAULT_DESIRED_MIN_CONN_INTERVAL,
                                           DEFAULT_DESIRED_MAX_CONN_INTERVAL,
                                           DEFAULT_DESIRED_SLAVE_LATENCY,
                                           DEFAULT_DESIRED_CONN_TIMEOUT,
                                           PeripheralTaskId);
       printf("654654\r\n");
      return (Events ^ BLUETOOTH_PARAM_UPDATE_EVENT);
    }

    if (Events & BLUETOOTH_READ_RSSI_EVENT)
    {
       printf("321321\r\n");
      GAPRole_ReadRssiCmd(PeripheralConnList.ConnHandle);
      return (Events ^ BLUETOOTH_READ_RSSI_EVENT);
    }

    if (Events & BLUETOOTH_PHY_UPDATE_EVENT)
    {
       printf("147147\r\n");
      PRINT("[bluetooth]: phy update: %X\r\n", GAPRole_UpdatePHY(PeripheralConnList.ConnHandle, 0, GAP_PHY_BIT_LE_2M, GAP_PHY_BIT_LE_2M, 0));
      return (Events ^ BLUETOOTH_PHY_UPDATE_EVENT);
    }

    if (Events & BLUETOOTH_SEND_DATA_TO_HOST_EVENT)
    {
      uint16_t NumberOfTransmit = 0;
      uint8_t *pSendDataBuff = &BluetoothDataBuff.DataBuff[BluetoothDataBuff.LengthOfTransmit];
      printf("send to phone \r\n");
      {
        NumberOfTransmit = MAX_BLUETOOTH_MTU_SIZE - 3;
        if ((BluetoothDataBuff.DataSize - BluetoothDataBuff.LengthOfTransmit) < NumberOfTransmit)
        {
          NumberOfTransmit = BluetoothDataBuff.DataSize - BluetoothDataBuff.LengthOfTransmit;
        }
      }

      {
 //       PeripheralNotify(pSendDataBuff, NumberOfTransmit);
         peripheralChar6Notify(pSendDataBuff,NumberOfTransmit);

        BluetoothDataBuff.LengthOfTransmit += NumberOfTransmit;
        if (BluetoothDataBuff.LengthOfTransmit < BluetoothDataBuff.DataSize)
        {
          tmos_start_task(PeripheralTaskId, BLUETOOTH_SEND_DATA_TO_HOST_EVENT, DEFAULT_BLUETOOTH_TRANSMIT_DELAY);
        }
        else
        {
          memset(&BluetoothDataBuff, 0, sizeof(BluetoothDataBuff_t));
        }
      }

      return (Events ^ BLUETOOTH_SEND_DATA_TO_HOST_EVENT);
    }

    if (Events & BLUETOOTH_UPDATING_THE_BROADCAST_PACKAGE_EVENT)
    {
      if (BluetoothGetConnectionStatus() != true)
      {
        PRINT("[bluetooth]: update bluetooth radio timeout.\r\n");

//        BluetoothStartGetBatteyLevel();
        BluetoothUpdatingTheBroadcastPackage();
      }

      printf("232323\r\n");

      tmos_start_task(PeripheralTaskId, BLUETOOTH_UPDATING_THE_BROADCAST_PACKAGE_EVENT, BluetoothGetUpdateRadioInterval());

      return (Events ^ BLUETOOTH_UPDATING_THE_BROADCAST_PACKAGE_EVENT);
    }
  }

  return 0;
}

void BluetoothUpdateRadioPackage(void)
{
  tmos_set_event(PeripheralTaskId, BLUETOOTH_UPDATING_THE_BROADCAST_PACKAGE_EVENT);
}

bool BluetoothSendDataToHost(const void *pDataIn, uint16_t DataSize)
{
  if (pDataIn != NULL)
  {
    if ((DataSize > 0) && (DataSize <= NOMINAL_PACKET_LENGTH))
    {
      printf("send to host \r\n");
      BluetoothDataBuff.DataSize = DataSize;
      memcpy(BluetoothDataBuff.DataBuff, pDataIn, DataSize);
      tmos_set_event(PeripheralTaskId, BLUETOOTH_SEND_DATA_TO_HOST_EVENT);

      return true;
    }
  }

  return false;
}


/*********************************************************************
 * @fn      peripheralParamUpdateCB
 *
 * @brief   Parameter update complete callback
 *
 * @param   connHandle - connect handle
 *          connInterval - connect interval
 *          connSlaveLatency - connect slave latency
 *          connTimeout - connect timeout
 *
 * @return  none
 */
 void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
                                    uint16_t connSlaveLatency, uint16_t connTimeout)
{
    if(connHandle == PeripheralConnList.ConnHandle)
    {
        PeripheralConnList.ConnInterval = connInterval;
        PeripheralConnList.ConnSlaveLatency = connSlaveLatency;
        PeripheralConnList.ConnTimeout = connTimeout;

        PRINT("Update %x - Int %x \n", connHandle, connInterval);
    }
    else
    {
        PRINT("ERR..\n");
    }
}


/*********************************************************************
 * @fn      peripheralChar4Notify
 *
 * @brief   Prepare and send simpleProfileChar4 notification
 *
 * @param   pValue - data to notify
 *          len - length of data
 *
 * @return  none
 */
void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
{
    attHandleValueNoti_t noti;
    if(PeripheralConnList.ConnHandle != GAP_CONNHANDLE_INIT)
    {
        noti.len = len;
        noti.pValue = GATT_bm_alloc(PeripheralConnList.ConnHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
        tmos_memcpy(noti.pValue, pValue, noti.len);
        if(simpleProfile_Notify(PeripheralConnList.ConnHandle, &noti) != SUCCESS)
        {
            PRINT("Notify ERR41 \n");
            GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI);
        }
    }
}

/*********************************************************************
 * @fn      peripheralChar1Notify
 *
 * @brief   Prepare and send simpleProfileChar4 notification
 *
 * @param   pValue - data to notify
 *          len - length of data
 *
 * @return  none
 */
void peripheralChar6Notify(uint8_t *pValue, uint16_t len)
{
    attHandleValueNoti_t noti;
    if(PeripheralConnList.ConnHandle != GAP_CONNHANDLE_INIT)
    {
        noti.len = len;
        noti.pValue = GATT_bm_alloc(PeripheralConnList.ConnHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
        tmos_memcpy(noti.pValue, pValue, noti.len);
        if(simpleProfile_Notify_Char6(PeripheralConnList.ConnHandle, &noti) != SUCCESS)
        {
            PRINT("Notify ERR6 \n");
            GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI);
        }
    }
}


